<?php

namespace Biz\model\activity;

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl;

class OutCouponConfig extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'outcoupon_config';


    //支持的券类型
    //对应到CouponConfig model的assume常量
    const SUPPORT_ASSUME_HHZ = 1;//好好住
    const SUPPORT_ASSUME_SHOP = 2;//商家专享


    //适用人群
    const PEOPLE_TYPE_NEW_USER = 1;//仅限新用户
    const PEOPLE_TYPE_ALL_USER = 2;//不限制用户


    //是否可领取
    const STATUS_CONFIG = 0;//待上线
    const STATUS_ONLINE = 1;//进行中
    const STATUS_OFFLINE = 2;//已结束
    const STATUS_DELETE = 9;//已删除


    //支持的券类型
    public static $supportAssumeDesc = [
        self::SUPPORT_ASSUME_HHZ  => '平台券',
        self::SUPPORT_ASSUME_SHOP => '商家券'
    ];


    //领取描述
    public static $statusDesc = [
        self::STATUS_CONFIG  => '待上线',
        self::STATUS_ONLINE  => '进行中',
        self::STATUS_OFFLINE => '已结束'
    ];

    public static $peopleTypeDesc = [
        self::PEOPLE_TYPE_NEW_USER => '仅限新用户',
        self::PEOPLE_TYPE_ALL_USER => '不限制新老用户',
    ];


    public static function formatInfo(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'support_assume':
                    $info['support_assume_desc'] = isset(self::$supportAssumeDesc[$value]) ? self::$supportAssumeDesc[$value] : '';
                    break;
                case 'people_type':
                    $info['people_type_desc'] = isset(self::$peopleTypeDesc[$value]) ? self::$peopleTypeDesc[$value] : '';
                    break;
                case 'status':
                    $info['status_desc'] = isset(self::$statusDesc[$value]) ? self::$statusDesc[$value] : '';
                    break;
                case 'template_config':
                    $info['template_data'] = json_decode($value, true);

                    if (!empty($info['template_data'])) {
                        $info['template_data'] = array_map(function ($v) {

                            if (!empty($v['pic_url'])) {
                                $v['pic_url_source'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_url'], ImgUrl::IMAGE_SIZE_OP_ORI);
                                $v['pic_url_thumb'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_url'], ImgUrl::IMAGE_SIZE_OP_ORI);
                            }

                            return $v;
                        }, $info['template_data']);
                    }
                    break;
            }
        }
    }

    public static function getInfo($id, $format = true)
    {
        $info = self::get('*', ['id' => $id]);

        if ($format)
            self::formatInfo($info);

        return $info;
    }

    public static function getInfoByIdMd5($idMd5)
    {
        $info = self::get('*', ['id_md5' => $idMd5]);

        self::formatInfo($info);

        return $info;
    }

    public static function changeStatus($id, $status)
    {
        $allowList = [
            self::STATUS_ONLINE  => self::STATUS_CONFIG,
            self::STATUS_OFFLINE => self::STATUS_ONLINE
        ];

        $ret = self::update(['status' => $status], ['id' => $id, 'status' => $allowList[$status]]);

        if (is_object($ret)) {
            return $ret->rowCount() > 0 ? true : false;
        }

        return false;
    }

    public static function createOrEdit($id, $data)
    {
        if ($id > 0) {

            self::update($data, ['id' => $id]);

            return $id;
        } else {
            $data['id_md5'] = md5(sprintf('id_md5_%d%s%s', $id, $data['start_time'], $data['stop_time']));

            self::insert($data);

            return self::id();
        }
    }

    public static function getOnlineOutCouponList($supportAssume){
        return self::select('*',['support_assume'=>$supportAssume,'status'=>self::STATUS_ONLINE]);
    }

    public static function getOnlineListByCouponId($couponId){
        return self::select('*',[
            'status'=>self::STATUS_ONLINE,
            'coupon_id_list[~]'=>["$couponId,%","%,$couponId,%","%,$couponId"]
        ]);
    }

    public static function setToOffline($id){
        $ret = self::update(['status' => self::STATUS_OFFLINE], ['id' => $id]);

        if (is_object($ret)) {
            return $ret->rowCount() > 0 ? true : false;
        }

        return false;
    }



}